package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import rs.mbs2.epics.server.entity.User;

@SuppressWarnings("serial")
public class LogoutController extends HttpServlet {

	private static Logger log = Logger.getLogger(LogoutController.class);
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		if(session != null) {
			User user = (User) session.getAttribute("user");
			log.info(new Date() + ": Izlogovan user " + user.getEmailAddress());
			request.getSession().invalidate();
		}
		
		request.getRequestDispatcher("/login.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
}
